﻿using Microsoft.AspNetCore.Mvc;
using SSO.Web2.Models;
using SSO.Web2.Utils;
using System.Text;

namespace SSO.Web2.Controllers;

/// <summary>
/// 用户信息
/// </summary>
public class AccountController (IHttpClientFactory httpClientFactory, CacheHelper cacheHelper) : Controller {
    /// <summary>
    /// 获取用户信息,有权限校验
    /// </summary>
    /// <returns></returns>
    [MyAuthorize]
    [HttpPost]
    public ResponseModel<UserDto> GetUserInfo () {
        ResponseModel<UserDto> user = new ResponseModel<UserDto>();
        return user;
    }

    /// <summary>
    /// 登录成功回调
    /// </summary>
    /// <returns></returns>
    public ActionResult LoginRedirect () {
        return View();
    }

    //根据authCode获取token
    [HttpPost]
    public async Task<ResponseModel<GetTokenDto>> GetAccessCode ([FromBody] GetAccessCodeRequest request) {
        //请求SSO获取 token
        var client = httpClientFactory.CreateClient();
        var param = new { authCode = request.authCode };
        string jsonData = System.Text.Json.JsonSerializer.Serialize(param);
        StringContent paramContent = new StringContent(jsonData);

        //请求sso获取token
        var response = await client.PostAsync("https://localhost:7000/SSO/GetToken",
            new StringContent(jsonData, Encoding.UTF8, "application/json"));
        string resultStr = await response.Content.ReadAsStringAsync();
        var result = System.Text.Json.JsonSerializer.Deserialize<ResponseModel<GetTokenDto>>(resultStr);
        if (result.code == 0) {
            //成功,缓存token到局部会话
            string token = result.data.token;
            string key = $"SessionCode:{request.sessionCode}";
            string tokenKey = $"token:{token}";
            cacheHelper.StringSet(key, token, TimeSpan.FromSeconds(result.data.expires));
            cacheHelper.StringSet(tokenKey, true, TimeSpan.FromSeconds(result.data.expires));
            Console.WriteLine($"获取token成功，局部会话code:{request.sessionCode},{Environment.NewLine}token:{token}");
        }

        return result;
    }

    /// <summary>
    /// 退出登录
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost]
    public ResponseModel LogOut ([FromBody] LogOutRequest request) {
        string key = $"SessionCode:{request.SessionCode}";
        //根据会话取出token
        string token = cacheHelper.StringGet<string>(key);
        if (!string.IsNullOrEmpty(token)) {
            //清除token
            string tokenKey = $"token:{token}";
            cacheHelper.DeleteKey(tokenKey);
        }

        Console.WriteLine($"会话Code:{request.SessionCode}退出登录");
        return new ResponseModel().SetSuccess();
    }
}